
/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：充值中心汇率表                                                    
*│　作    者：gaotang                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-02-20 15:07:13                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： BestTrader.Cms.Services                                  
*│　类    名： UCExchangeRateService                                    
*└──────────────────────────────────────────────────────────────┘
*/
using BestTrader.Cms.Core.Extensions;
using BestTrader.Cms.IRepository;
using BestTrader.Cms.IServices;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using BestTrader.Cms.ViewModels;
using BestTrader.Cms.Models;
using AutoMapper;
using Microsoft.Extensions.Logging;

namespace BestTrader.Cms.Services
{
    public class UCExchangeRateService : IUCExchangeRateService
    {
        private readonly IUCExchangeRateRepository _repository;
        private readonly IMapper _mapper;
        private readonly ILogger<UCExchangeRateService> _logger;


        public UCExchangeRateService(IUCExchangeRateRepository repository, IMapper mapper, ILogger<UCExchangeRateService> logger)
        {
            _repository = repository;
            _mapper = mapper;
            _logger = logger;
        }

        public TableDataResult<IEnumerable<UCExchangeRate>> GetUCExchangeRateList()
        {
            var result = new TableDataResult<IEnumerable<UCExchangeRate>>();
            string conditions = " WHERE  IsDelete=0";

            var data = _repository.GetList(conditions);
            result.Data = data;
            return result;

        }


        public TableDataModel LoadData(UCExchangeRateRequestModel model, String UserId, String RoleId)
        {
            var viewList = new List<UCExchangeRate>();

            string conditions = " WHERE IsDelete = 0  ";
            var list = _repository.GetListPaged(model.Page, model.Limit, conditions, "Id desc").ToList();

            return new TableDataModel
            {
                count = list.Count(),
                data = list,
            };

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <param name="UserId"></param>
        /// <returns></returns>

        public BaseResult AddOrModify(UCExchangeRateModel model, string UserId)
        {
            var result = new BaseResult();
            UCExchangeRate entity;
            var now = DateTime.Now;
            var _managerId = 0;
            if (int.TryParse(UserId, out _managerId))
            {
                if (model.Id > 0)
                {
                    entity = _repository.Get(model.Id);

                    _logger.LogTrace($"充值中心出入金汇率被编辑 UserId：{UserId},ExChangeRateId:{model.Id}", entity);

                    if (entity != null)
                    {
                        entity.IsDefault = model.IsDefault;
                        entity.ProjectCode = model.ProjectCode;
                        entity.ProjectName = model.ProjectName;
                        entity.ProjectPrice = model.ProjectPrice;
                        if (_repository.Update(entity) > 0)
                        {
                            result.ResultCode = ResultCodeAddMsgKeys.CommonObjectSuccessCode;
                            result.ResultMsg = ResultCodeAddMsgKeys.CommonObjectSuccessMsg;
                        }
                        else
                        {
                            result.ResultCode = ResultCodeAddMsgKeys.CommonExceptionCode;
                            result.ResultMsg = ResultCodeAddMsgKeys.CommonExceptionMsg;
                        }
                    }
                    else
                    {
                        result.ResultCode = ResultCodeAddMsgKeys.CommonFailNoDataCode;
                        result.ResultMsg = ResultCodeAddMsgKeys.CommonFailNoDataMsg;
                    }
                }
                else
                {
                    entity = _mapper.Map<UCExchangeRate>(model);
                    entity.ProjectTime = now;
                    entity.AddManagerId = _managerId;
                    entity.IsDefault = false;
                    entity.IsDelete = false;
                    entity.AddTime = now;
                    if (_repository.Insert(entity) > 0)
                    {
                        result.ResultCode = ResultCodeAddMsgKeys.CommonObjectSuccessCode;
                        result.ResultMsg = ResultCodeAddMsgKeys.CommonObjectSuccessMsg;
                    }
                    else
                    {
                        result.ResultCode = ResultCodeAddMsgKeys.CommonExceptionCode;
                        result.ResultMsg = ResultCodeAddMsgKeys.CommonExceptionMsg;
                    }


                }
            }
            return result;

        }
        /// <summary>
        /// 获取出入金汇率
        /// </summary>
        /// <returns></returns>


        public BaseResult DeleteId(int Id, string UserId)
        {
            var result = new BaseResult();
            if (Id == 0)
            {
                result.ResultCode = ResultCodeAddMsgKeys.CommonModelStateInvalidCode;
                result.ResultMsg = ResultCodeAddMsgKeys.CommonModelStateInvalidMsg;
            }
            else
            {
                var entity = _repository.Get(Id);
                if (entity != null)
                {
                    entity.IsDelete = true;
                    var count = _repository.Update(entity);

                    if (count > 0)
                    {
                        _logger.LogTrace($"充值中心汇率表被删除 UserId：{UserId} ExchangeRateId:{Id}");
                        //成功
                        result.ResultCode = ResultCodeAddMsgKeys.CommonObjectSuccessCode;
                        result.ResultMsg = ResultCodeAddMsgKeys.CommonObjectSuccessMsg;
                    }
                    else
                    {
                        //失败
                        result.ResultCode = ResultCodeAddMsgKeys.CommonExceptionCode;
                        result.ResultMsg = ResultCodeAddMsgKeys.CommonExceptionMsg;
                    }
                }
                else
                {
                    //数据为空
                    result.ResultCode = ResultCodeAddMsgKeys.CommonFailNoDataCode;
                    result.ResultMsg = ResultCodeAddMsgKeys.CommonFailNoDataMsg;

                }
            }
            return result;
        }









    }
}